<?php
/**
 * Created by .
 * User: luogan
 * Date: 2022/1/17
 */

namespace app\admin\service;


use app\exception\ErrorException;
use app\model\DeptModel;

class DeptService extends BaseService
{
    /**
     * 获取部门列表
     * @param $data
     * @return array
     * @throws ErrorException
     */
    public function getList($data)
    {
        $result =(new DeptModel)->getDeptTreeList($data);
        if (!$result){
            throw new ErrorException();
        }
        return $result;
    }

    /**
     * 部门新增
     * @param $data
     * @return DeptModel|\think\Model
     */
    public function create($data)
    {

        $result = DeptModel::create($data);
        if (!$result){
            throw new ErrorException();
        }
        admin_log('新增部门：'.$data['name'].' 部门id '.$result);
        return $result;
    }

    /**
     * 部门编辑
     * @param $data
     * @return DeptModel|\think\Model
     */
    public function update($data)
    {

        $result = DeptModel::where('id',$data['id'])->update($data);
        if (!$result){
            throw new ErrorException();
        }
        admin_log('编辑部门：部门id '.$data['id']);
        return $result;
    }

    /**
     * 获取部门详情
     * @param $id
     * @return DeptModel|array|mixed|\think\Model
     */
    public function getInfo($id)
    {
        $result = DeptModel::find($id);
        if (!$result){
            throw new ErrorException();
        }
        return $result;
    }

    /**
     * 部门删除
     * @param $id
     * @return bool
     */
    public function delete($id)
    {
        $result = DeptModel::destroy($id);
        if (!$result){
            throw new ErrorException();
        }
        return $result;
        admin_log('删除部门：部门id'.$id);
    }
}
